class Solution:
    def findOrder(self, numCourses: int, prerequisites: list) -> list:
        mid_list = [[0, [], i] for i in range(numCourses)]
        for lists in prerequisites:
            mid_list[lists[0]][0] += 1
            mid_list[lists[1]][1].append(lists[0])
        search_list = [mid_list[i] for i in range(numCourses) if mid_list[i][0] == 0]
        res = []
        while search_list:
            lists = search_list.pop(0)
            res.append(lists[2])
            for num in lists[1]:
                mid_list[num][0] -= 1
                if mid_list[num][0] == 0:
                    search_list.append(mid_list[num])
        if len(res) != numCourses:
            return []
        return res


a = Solution()
print(a.findOrder(2, [[1, 0]]))
print(a.findOrder(4, [[1, 0], [2, 0], [3, 1], [3, 2]]))
